/* @(#)GSManufactureQueryListAction.java    V0.1
 *
 * Project: GSdevelop
 *
 * Modify Information:
 * =============================================================================
 *   Author       Date       Description
 *   ------------ ---------- ---------------------------------------------------
 *   owen         2006-3-22
 *
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2006 Beijing HiTRUST Technology Co., Ltd. 1808 Room, 
 *        Science & Technology Building, No. 9 South Street, Zhong Guan Cun, 
 *        Haidian District, Beijing ,100081, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with HiTRUST.
 *
 * Warning:
 * =============================================================================
 * 
 */
package com.hitrust.gs.action;

import java.util.ArrayList;

import system.LoginContext;
import system.base.ProductTypeManager;

import com.hitrust.gs.business.GSManufactureProcessor;
import com.hitrust.gs.business.GSUniteGwProcessor;
import com.hitrust.gs.form.GSManufactureQueryListForm;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.service.GSBaseAction;
import com.hitrust.gs.service.GSBaseForm;
import com.hitrust.gs.service.GSBaseManager;
import com.hitrust.gs.util.exception.GSException;
import com.hitrust.gs.util.page.ResultProcess;
import com.hitrust.gs.vo.GSManufactureVO;
import com.hitrust.gs.util.date.DateUtil;

import framework.core.DBConnection;
import framework.core.ObjectList;

/**
 * 查询产品信息
 */
public class GSManufactureQueryListAction extends GSBaseAction{

    public String execute(GSBaseForm aForm) throws Exception {
        GSManufactureQueryListForm tForm = (GSManufactureQueryListForm)aForm;
        GSManufactureProcessor tGSManufProcessor = new GSManufactureProcessor();
        GSUniteGwProcessor unitePro=new GSUniteGwProcessor();
        //取得页面参数放入VO对象
        LoginContext context=(LoginContext) aForm.getSession().getAttribute(GSBaseConstants.LOGIN);
        GSManufactureVO tGSManufactureVO = new GSManufactureVO();
        tGSManufactureVO.gs_concentricity_id = tForm.getConcentricityId();
        tGSManufactureVO.gs_manufacture_name = tForm.getManufactureName();
        tGSManufactureVO.gs_manufacturer_name = tForm.getManufacturerName();
        tGSManufactureVO.gs_manufacture_category1 = tForm.getManuftype1();
        tGSManufactureVO.gs_manufacture_category2 = tForm.getManuftype2();
        tGSManufactureVO.gs_manufacture_category3 = tForm.getManuftype3();
        tGSManufactureVO.gs_manufacture_brand = tForm.getManufactureBrand();
        tGSManufactureVO.gs_manufacture_spec = tForm.getManufactureSpec();
        tGSManufactureVO.gs_manufacture_type = tForm.getManufactureType();
        tGSManufactureVO.gs_is_tradeflat_manufacture = tForm.getIstradeflatmanufacture();
        tGSManufactureVO.gs_provider_short_name = tForm.getProvider();
        tGSManufactureVO.gs_drawing_peice_code = tForm.getDrawingpiececode();
		tGSManufactureVO.is_buy = tForm.getIsBuy();
		tGSManufactureVO.gs_balance_mode = tForm.getBalancemode();
		tGSManufactureVO.warehousecode=tForm.getWarehousecode();
		tGSManufactureVO.gsValidity = tForm.getGsValidity();
		tGSManufactureVO.is_normal = tForm.getIsNormal();  
		tGSManufactureVO.upperType = tForm.getUpperType();//区域分类
		tGSManufactureVO.productType=tForm.getProductType();//招标分类
		tGSManufactureVO.is_pic=tForm.getIsPic();//有无图片
		tGSManufactureVO.is_ykck=tForm.getIsYkCk();//压库不可采购是否出库
		boolean flag = false;
		if(tForm.getIstradeflatmanufacture().trim().equals("2"))
		   flag =true;
		
		tGSManufactureVO.provider_status = tForm.getProviderStatus();
		ArrayList tmplist=new ArrayList();
		if (tGSManufactureVO.warehousecode.equals("")) {
            tmplist= unitePro.getBzwzAndTmpWareHouse(context.getLoginWarehouseCode());
        }
		tGSManufactureVO.setTmplist(tmplist);
		String[] tDateStr = new String[2];
		
		tDateStr[0] = tForm.getManufactureRecordDateStr();
		tDateStr[1] = tForm.getManufactureRecordDateEnd();
		
		if(tForm.getManufactureRecordDateStr() == null){
			tDateStr[0] = "00000000";
		}
		if(tForm.getManufactureRecordDateEnd() == null){
			tDateStr[1] = "99999999";		
		}
		tDateStr = DateUtil.formateDate(tDateStr);
		
		tGSManufactureVO.manufactureRecordDateStr = tDateStr[0];
		tGSManufactureVO.manufactureRecordDateEnd = tDateStr[1];
		//tGSManufactureVO.gsValidity = "1";
        
        String keyword = tForm.getKeyword();
        String iskeywordquery = (String)aForm.getRequest().getParameter("iskeywordquery");
        aForm.getRequest().setAttribute("iskeywordquery",iskeywordquery);
        
        
        try{
        	String tableName="";
        	if(!"".equals(tForm.getUpperType())){
        	tableName=tGSManufProcessor.initDataForArea(tForm.getUpperType());
        	}
            //根据页面传入参数取得产品信息
            ArrayList tList = null;           
            String[]  tStr = this.getPageParams(aForm,tGSManufactureVO,keyword,iskeywordquery,tmplist,tableName);
            String sqlbuf="";
            if("F".equals(iskeywordquery)){
                sqlbuf=tGSManufProcessor.getSqlDataForProductManage(tGSManufactureVO,tableName);
                
                if(!"".equals(tForm.getUpperType())){
                    aForm.getSession().setAttribute("useTempTable",tableName);
                    aForm.getSession().setAttribute("useUpperType",tForm.getUpperType());
                }
                
                if(flag){
                    tList = tGSManufProcessor.getManufactureByData(tGSManufactureVO,Integer.parseInt(tStr[0]),Integer.parseInt(tStr[1]),tableName);
                    //tStr = this.getPageParams(aForm,tList.size(),keyword,iskeywordquery,tmplist);
                }else{
                    tList = tGSManufProcessor.getManufactureByData(tGSManufactureVO,Integer.parseInt(tStr[0]),Integer.parseInt(tStr[1]),tableName);
                }
               
                
            }
            else{
                GSManufactureVO vo = new GSManufactureVO();
                //vo.gsValidity="1";
                vo.gs_manufacture_brand = keyword;
                vo.gs_manufacture_name = keyword;
                vo.gs_manufacture_spec = keyword;
                vo.gs_manufacture_type = keyword;
                vo.gs_manufacturer_name = keyword;
                vo.gs_appender_property = keyword;
                vo.setTmplist(tmplist);
                
                sqlbuf = tGSManufProcessor.getSqlForKeywordOther(vo,keyword);
                tList = tGSManufProcessor.getManufactureByKeywordForOther(keyword,tmplist,Integer.parseInt(tStr[0]),Integer.parseInt(tStr[1]));
            }
            aForm.getSession().setAttribute("querycond", sqlbuf);
       
            //放入ResultProcess对象为页面分页做准备
            ResultProcess tProc = new ResultProcess();
            tProc.setResult(tList);
            tForm.getRequest().setAttribute("manufacture",tProc);
            //存储分页参数
            tForm.getRequest().setAttribute("pageSize",tStr[1]);
            tForm.getRequest().setAttribute("totalRecords",tStr[2]);
            
        	if(!"".equals(tForm.getUpperType())){
            tGSManufProcessor.dropTableForArea(tableName);
        	}
        	
            //取得产品等级为1级的产品种类
            ArrayList tManuf1 = tGSManufProcessor.getManufactureType("1");
            tForm.getRequest().setAttribute("manufacture1",tManuf1);
            //取得产品等级为2级的产品种类
            ArrayList tManuf2 = tGSManufProcessor.getManufactureType("2");
            aForm.getRequest().setAttribute("manufacture2",tManuf2);
            //取得产品等级为3级的产品种类
            ArrayList tManuf3 = tGSManufProcessor.getManufactureType("3");
            aForm.getRequest().setAttribute("manufacture3",tManuf3);
            
            DBConnection con =null;
            try {
            	 con = DBConnection.open();
                 ObjectList productTypeManager1 = ProductTypeManager.getUpperProductType(con);
                 ObjectList productTypeManager2 = ProductTypeManager.getProductType(con);
                 
                 aForm.getRequest().setAttribute("productTypeManager1",productTypeManager1);
                 
                 aForm.getRequest().setAttribute("productTypeManager2",productTypeManager2);
    		} catch (Exception e) {
    			GSLogWriter.error("产品结算分类异常",e);
    		}finally{
    			if(con!=null){
    				con.close();
    			}
    		}
            //种类等级
            tGSManufProcessor.getManufactureOfLevel(aForm);
            
            tForm.getRequest().setAttribute("istradeflatmanufacture",tForm.getIstradeflatmanufacture());
            tForm.getRequest().setAttribute("gsValidity", tForm.getGsValidity());
            tForm.getRequest().setAttribute("productType", tForm.getProductType());
            //增加配置文件 判断湖南巴士计划价 2009-06-04
            if(GSBaseConstants.PLANPRICE.equals("true")){
                aForm.getRequest().setAttribute("isPlanprice", "1");
            }else{
                aForm.getRequest().setAttribute("isPlanprice", "0");
            }
            
            tForm.getRequest().setAttribute("upperType", tGSManufactureVO.getUpperType());
            tForm.getRequest().setAttribute("productType", tGSManufactureVO.getProductType());
            tForm.getRequest().setAttribute("providerStatus", tForm.getProviderStatus());
          //仓库
            ArrayList wareHouse= unitePro.getWareHouseLevelName(context.getWarehouse().getCode(),context.getWarehouse().getCode());
            aForm.getRequest().setAttribute("wareHouse", wareHouse);
        }catch(GSException e){
            tForm.getRequest().setAttribute("action","A400301.do");
            throw e;
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            tForm.getRequest().setAttribute("action","A400301.do");
            throw new GSException(GSException.E10030,GSException.E10030_MSG);
        }
        return GSBaseConstants.FORWARD_SUCCESS;
    }

    /**
     * 取得分页参数
     * @param aForm         基类form
     * @param aOrderVO      订单VO
     * @param aOrg          组织ArrayList对象
     * @return
     * @throws Exception
     */
    private String[] getPageParams(GSBaseForm aForm,GSManufactureVO tGSManufactureVO,String keyword,String iskeywordquery,ArrayList tmplist,String tableName) throws Exception{
        String tStr[] = new String[3];
        int totalRecords;
        int pageNo;
        int pageSize;
        String tPage = GSBaseConstants.tPage;
        GSManufactureProcessor tProc = new GSManufactureProcessor();
        //根据页面传入的查询参数查询订单信息
        if(aForm.getChoice() == null || aForm.getChoice().equals("")){
            if("F".equals(iskeywordquery)){                
                totalRecords = tProc.getManufactureCountByData(tGSManufactureVO,tableName);
            }
            else{ 
                totalRecords = tProc.getManufactureCountByKeywordForOther(keyword,tmplist);
            }
            
            // 取得系统默认的页大小
            pageSize = Integer.parseInt(tPage);
            // PageNo=1
            pageNo = 1;
            int n = (totalRecords % pageSize == 0 )? totalRecords / pageSize:totalRecords / pageSize + 1;
            if (pageNo>n) {
                pageNo=n;
            }
            tStr[0] = "" + pageNo;
            tStr[1] = "" + pageSize;
            tStr[2] = "" + totalRecords;
        }else{
            tStr = GSBaseManager.getPageNoAndPageSize(aForm);
        }
        return tStr;
    }

    private String[] getPageParams(GSBaseForm aForm,int records,String keyword,String iskeywordquery,ArrayList tmplist) throws Exception{
        String tStr[] = new String[3];
        int totalRecords;
        int pageNo;
        int pageSize;
        String tPage = GSBaseConstants.tPage;
        GSManufactureProcessor tProc = new GSManufactureProcessor();
        //根据页面传入的查询参数查询订单信息
        if(aForm.getChoice() == null || aForm.getChoice().equals("")){
            if("F".equals(iskeywordquery)){                
                totalRecords = records;
            }
            else{ 
                totalRecords = tProc.getManufactureCountByKeywordForOther(keyword,tmplist);
            }
            
            // 取得系统默认的页大小
            pageSize = Integer.parseInt(tPage);
            // PageNo=1
            pageNo = 1;
            int n = (totalRecords % pageSize == 0 )? totalRecords / pageSize:totalRecords / pageSize + 1;
            if (pageNo>n) {
                pageNo=n;
            }
            tStr[0] = "" + pageNo;
            tStr[1] = "" + pageSize;
            tStr[2] = "" + totalRecords;
        }else{
            tStr = GSBaseManager.getPageNoAndPageSize(aForm);
        }
        return tStr;
    }
}
